Function Delete-Cosm-Data {
	param(
		[Parameter(Mandatory=$true,Position=0)][string]$apikey
		,[Parameter(Mandatory=$true,Position=1)][string]$feedid
		,[Parameter(Mandatory=$true,Position=2)][string]$datastreamId
		,[Parameter(Mandatory=$false)]$start=$null
		,[Parameter(Mandatory=$false)]$end=$null
		,[Parameter(Mandatory=$false)]$duration=$null
		,[Parameter(Mandatory=$false)]$timezone=$null
)
	Begin {
		Write-Verbose "$($MyInvocation.MyCommand.Name):: Function started" 
		
		#------------------
		# Create Web Client 
		#------------------
		$local:pbWC = WebClient -baseaddress "http://api.Cosm.com/v2/feeds/"
		#$pbWC.Headers.Add("Content-Type", "application/xml")
		$pbWC.Headers.Add("X-CosmApiKey",$apikey)

		$pbQueryHT = new-object System.Collections.Specialized.NameValueCollection
	}
		
	Process {	Write-Verbose "$($MyInvocation.MyCommand.Name):: Process started"
		try {
			$pbUriSTR=[string]::Concat(  $feedid, 	"/datastreams/", $datastreamId, "/datapoints")       
			write-Host "Deleting Cosm History datapoints from Feed $FeedId Datastream $datastreamId"
			[String]$EndDeleteUtcSTR=get-Date $end -f u #format into UTC string
			[String]$StartDeleteUtcSTR=get-Date $start -f u #format into UTC string

			#------------------------------
			# Set Query parameters
			#------------------------
			$pbQueryHT.clear() # empty the query string
			if ($start -ne $null)	{$pbQueryHT.Add("start", $StartDeleteUtcSTR) } #all data points after $pbExtractStartUtcSTR
			if ($end -ne $null)	{$pbQueryHT.Add("end", $EndDeleteUtcSTR)  	}#and before $ pbExtractEndUtcSTR
			if ($duration -ne $null) {$pbQueryHT.Add("duration",$duration)}  	#or for duration
			$pbWC.QueryString = $pbQueryHT 	# Attach QueryString to the WebClient.
			#write-Host $pbQueryHT[$pbQueryHT.keys]
			write-debug "pbUriSTR=$([string]::Concat($pbWC.baseaddress, $pbUriSTR))"
			try {
				# 
				$pbResponseSTR = $pbWC.UploadString($pbUriSTR, "DELETE", "") 
				$pbResponseHeaders=$pbWC.ResponseHeaders
				}
			catch {
				write-Host "problem deleting data from Cosm $_ `n $StackTrace"
			}
			 finally {
			 	$pbResponseSTR
			 }
		}
		catch {
			Throw "$($MyInvocation.MyCommand.Name):: Unknown Problem "
		}
	}
	End {
		Write-Verbose "$($MyInvocation.MyCommand.Name):: Function ended"
		#Return $pbExtractedSTR
		$pbWC.dispose
	} 
}

